package com.example.mybatisfinaltask.controller;

import com.example.mybatisfinaltask.pojo.Emp;
import com.example.mybatisfinaltask.pojo.Result;
import com.example.mybatisfinaltask.service.EmpService;
import com.example.mybatisfinaltask.utils.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDate;
import java.time.LocalDateTime;

@Slf4j
@RestController
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping("/emps")
    Result list(@RequestParam(defaultValue = "1") Integer page,
                @RequestParam(defaultValue = "10") Integer pageSize,
                String name,Short gender,
                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end
                ){
        log.info("分页查询，参数为:{}，{}",page,pageSize);
        return Result.success(empService.pageSelect(page,pageSize,name,gender,begin,end));
    }
    @DeleteMapping("/emps/{ids}")
    Result batchDelete(@PathVariable Integer[]ids){
        empService.batchDelete(ids);
        log.info("批量删除员工 id为");
        for (Integer id : ids) {
            log.info("{} ",id);
        }
        return Result.success();
    }
    @PostMapping("/emps")
    Result add(@RequestBody Emp emp){
        empService.add(emp);
        log.info("添加员工 {}",emp);
        return Result.success();
    }
    @PutMapping("/emps")
    Result modify(@RequestBody Emp emp){
        log.info("修改员工");
        System.out.println(emp);
        empService.modify(emp);
        return Result.success();
    }
    @GetMapping("/emps/{id}")
    Result getById(@PathVariable Integer id){
        log.info("查询员工，id为{}",id);
        return Result.success(empService.getById(id));
    }

}
